#!/usr/bin/python
# -*- coding: UTF-8 -*-
import datetime
from django import template
from django.template.defaultfilters import stringfilter
from django.utils.html import conditional_escape
from django.utils.safestring import mark_safe

register = template.Library()  # register的名字是固定的,不可改变


@register.filter
def my_filter(v1, v2):
    return v1 * v2


@register.filter(name='cut')
def cut(value, arg):
    return value.replace(arg, '')


@register.filter
@stringfilter
def lower(value):
    return value.lower()


@register.filter(expects_localtime=True)
def businesshours(value):
    try:
        return 9 <= value.hour < 17
    except AttributeError:
        return ''


@register.filter(needs_autoescape=True)
def initial_letter_filter(text, autoescape=True):
    first, other = text[0], text[1:]
    if autoescape:
        esc = conditional_escape
    else:
        esc = lambda x: x
    result = '<strong>%s</strong>%s' % (esc(first), esc(other))
    return mark_safe(result)


@register.simple_tag
def current_time(format_string):
    return datetime.datetime.now().strftime(format_string)
